use anyhow::Result;
use rand::Rng;

/// 生成随机字符串
///
/// # example:
///
/// ```
/// use self::get_rang_str;
///
/// println!(cores::get_rang_str(32).unwrap())
/// ```
///
pub fn get_rang_str(len: usize) -> Result<String> {
    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
                                 abcdefghijklmnopqrstuvwxyz\
                                 0123456789)(*&^%$#@!~";
    let mut rng = rand::thread_rng();
    let rang_str: String = (0..len)
        .map(|_| {
            let idx = rng.gen_range(0..CHARSET.len());
            CHARSET[idx] as char
        })
        .collect();
    Ok(rang_str)
}